int depth(struct TreeNode* root, int* flag)
{
    if (root == NULL)
    {
        return 0;
    }
    if (root->left == root->right)
    {
        return 1;
    }
    int leftdepth = depth(root->left, flag);
    int rightdepth = depth(root->right, flag);
    if (leftdepth > rightdepth)
    {
        if (leftdepth - rightdepth > 1)
        {
            *flag = 0;
        }
    }
    else
    {
        if (rightdepth - leftdepth > 1)
        {
            *flag = 0;
        }
    }
    return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1;
}

bool isBalanced(struct TreeNode* root) {
    int i = 1;
    depth(root, &i);
    if (i == 0)
    {
        return false;
    }
    else
        return true;
}